package 回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tmh
 * @date 2024/9/12 17:48
 * @description
 */
public class T131分割回文串 {
    List<List<String>> result = new ArrayList<>();
    List<String> path = new ArrayList<>();

    public List<List<String>> partition(String s) {
        backTracking(s, 0);
        return result;
    }

    public void backTracking(String s, int beginStart) {
        //中止条件
        if (beginStart>=s.length()){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = beginStart; i < s.length(); i++) {
            if (isPalindrome(s, beginStart, i)) {
                String str = s.substring(beginStart, i + 1);
                path.add(str);
            } else {
                continue;
            }
            backTracking(s, i + 1);
            path.remove(path.size() - 1);
        }
    }

    //判断是否是回文字符串
    private boolean isPalindrome(String s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
